package cn.shyouchi.wuguanjia.android.ui.home.security.thread.contact;

import android.os.Parcel;
import android.os.Parcelable;

import com.iasii.app.citylist.utils.PingYinUtil;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Eenie on 2016/12/30 at 11:22
 * Email: 472279981@qq.com
 * Des:
 */

public class Contacter  implements Parcelable{
    /**
     * id : 1
     * name : 唐海斌
     * phone : 13800138004
     * id_card_head_image : /usr/local/EC/youchi/images/user/20161104/20161104014632YC954564236.jpg
     * duties : 项目经理
     */

    private int id;
    private String name;
    private String phone;
    private String id_card_head_image;
    private String duties;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getId_card_head_image() {
        return id_card_head_image;
    }

    public void setId_card_head_image(String id_card_head_image) {
        this.id_card_head_image = id_card_head_image;
    }

    public String getDuties() {
        return duties;
    }

    public void setDuties(String duties) {
        this.duties = duties;
    }


    public String getPinyin() {
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(name);
        if (m.find()) {
            return PingYinUtil.getPingYin(name);
        }
        return name;
    }


    public String getFirstLeater() {
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(name);
        if (m.find()) {
            return PingYinUtil.getPingYin(name).substring(0, 1).toUpperCase();
        }
        return "#";
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.name);
        dest.writeString(this.phone);
        dest.writeString(this.id_card_head_image);
        dest.writeString(this.duties);
    }

    public Contacter() {

    }

    protected Contacter(Parcel in) {
        this.id = in.readInt();
        this.name = in.readString();
        this.phone = in.readString();
        this.id_card_head_image = in.readString();
        this.duties = in.readString();
    }

    public static final Parcelable.Creator<Contacter> CREATOR = new Parcelable.Creator<Contacter>() {
        @Override
        public Contacter createFromParcel(Parcel source) {
            return new Contacter(source);
        }

        @Override
        public Contacter[] newArray(int size) {
            return new Contacter[size];
        }
    };


    public String getPingYin() {


        return getPingYin(name);
    }


    private String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        Pattern p = Pattern.compile("^[\u4E00-\u9FA5A-Za-z_]+$");
        Matcher matcher = p.matcher(inputString.substring(0, 1));
        if (matcher.find()) {
            char[] input = inputString.trim().toCharArray();
            String output = "";
            try {
                for (int i = 0; i < input.length; i++) {
                    if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                        output += temp[0];
                    } else
                        output += Character.toString(input[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            return output;
        } else {
            return "";
        }
    }


}
